/*==============================================================================
FILE NAME: Figure_A7.do
CREATED: 22 July 2025
==============================================================================*/

**Figure A9

/* Set directory if working independently through code
if c(username)=="" { //insert username
	global rootdir "" // insert root path
	global processed_data "$rootdir/processed_data" 
	global figures "$rootdir/output/figures"  // Define global paths for replication package
} 
*/

//count of other air complaint in same region
use "$processed_data/Air_Panel.dta", clear

drop if never_air_inv == 1

egen t = group(year month)
xtset RN_id t

//Generate air complaint investigation variables for the number of days of investigatoin
forv h = 0/12 {
	gen day_air_complaint_inv_`h' = f`h'.day_air_complaint_inv - l1.day_air_complaint_inv
}

forv h = 2/12 {
	gen day_air_complaint_inv_neg`h' = l`h'.day_air_complaint_inv - l1.day_air_complaint_inv
}

egen RN_year = group(RN year)

// Keep certain variables
keep RN_id t RN_year region_id day_air_complaint_inv day_air_nocomplaint_inv incident other_air_complaint_region SIC_2digit1 ind_air_complaint

xtset RN_id t

//Reinitialize storage variables
cap drop b u d se Years Zero
gen Years = _n-13 if _n<=37
gen Zero = 0 if _n <=37
gen b = 0
gen se = 0
gen u = 0 
gen d = 0
** facility-level aggregation:

xtset RN_id t
foreach y in day_air_complaint_inv day_air_nocomplaint_inv {
forv h = 0/12 {
gen `y'_`h' = f`h'.`y' - l1.`y'
}
forv h = 2/12 {
gen `y'_neg`h' = l`h'.`y' - l1.`y'
}
}


// Regressions 
foreach h in 0 1 2 3 4 5 6 7 8 9 10 11 12 {
reghdfe day_air_nocomplaint_inv_`h' incident other_air_complaint_region, absorb(RN_year t) cluster(region_id)

replace b = _b[other_air_complaint_region]*1394 if Years == `h'
replace se = _se[other_air_complaint_region]*1394 if Years == `h'
replace u = ((_b[other_air_complaint_region]*1394) + 1.96*(_se[other_air_complaint_region]*1394)) if Years == `h'
replace d = ((_b[other_air_complaint_region]*1394) - 1.96*(_se[other_air_complaint_region]*1394)) if Years == `h'
}
	
foreach h in 2 3 4 5 6 7 8 9 10 11 12 {
reghdfe day_air_nocomplaint_inv_neg`h' incident other_air_complaint_region, absorb(RN_year t) cluster(region_id)
replace b = _b[other_air_complaint_region]*1394 if Years == -`h'
replace se = _se[other_air_complaint_region]*1394 if Years == -`h'
replace u = ((_b[other_air_complaint_region]*1394) + 1.96*(_se[other_air_complaint_region]*1394)) if Years == -`h'
replace d = ((_b[other_air_complaint_region]*1394) - 1.96*(_se[other_air_complaint_region]*1394)) if Years == -`h'
}	

preserve
keep if Years != . 
drop if Years > 12
keep b u d se Years Zero
export delimited "$point_estimates/Point_Estimates_Figure_A9_Panel_A.csv", replace
graph set window fontface "Times New Roman"
twoway(rarea u d Years, col(navy) fint(inten20) lwidth(0) lpattern(solid))(line b Years, lcolor(navy) lpattern(solid) lwidth(medium))(line Zero Years, lcolor(black)), xlabel(-12(1)12, nogrid labsize(vlarge)) ylabel(-0.4(0.1)0.4, labsize(vlarge)) legend(off) ytitle("{&Delta} # of Non-Complaint" "Investigation Days", size(vlarge)) xtitle("Month", size(vlarge)) graphregion(color(white)) plotregion(color(white)) xsize(8.6)
graph export "$figures/Figure_A9_Panel_A.pdf", replace

restore


foreach h in 0 1 2 3 4 5 6 7 8 9 10 11 12 {
reghdfe day_air_complaint_inv_`h' incident other_air_complaint_region, absorb(RN_year t) cluster(region_id)
replace b = _b[other_air_complaint_region]*1394 if Years == `h'
replace se = _se[other_air_complaint_region]*1394 if Years == `h'
replace u = ((_b[other_air_complaint_region]*1394) + 1.96*(_se[other_air_complaint_region]*1394)) if Years == `h'
replace d = ((_b[other_air_complaint_region]*1394) - 1.96*(_se[other_air_complaint_region]*1394)) if Years == `h'
}
	
foreach h in 2 3 4 5 6 7 8 9 10 11 12 {
reghdfe day_air_complaint_inv_neg`h' incident other_air_complaint_region, absorb(RN_year t) cluster(region_id)
replace b = _b[other_air_complaint_region]*1394 if Years == -`h'
replace se = _se[other_air_complaint_region]*1394 if Years == -`h'
replace u = ((_b[other_air_complaint_region]*1394) + 1.96*(_se[other_air_complaint_region]*1394)) if Years == -`h'
replace d = ((_b[other_air_complaint_region]*1394) - 1.96*(_se[other_air_complaint_region]*1394)) if Years == -`h'
}	

preserve
keep if Years != . 
drop if Years > 12
keep b u d se Years Zero
export delimited "$point_estimates/Point_Estimates_Figure_A9_Panel_B.csv", replace
graph set window fontface "Times New Roman"
twoway(rarea u d Years, col(navy) fint(inten20) lwidth(0) lpattern(solid))(line b Years, lcolor(navy) lpattern(solid) lwidth(medium))(line Zero Years, lcolor(black)), xlabel(-12(1)12, nogrid labsize(vlarge)) ylabel(-0.4(0.1)0.4, labsize(vlarge)) legend(off) ytitle("{&Delta} # of Complaint" "Investigation Days", size(vlarge)) xtitle("Month", size(vlarge)) graphregion(color(white)) plotregion(color(white)) xsize(8.6)
graph export "$figures/Figure_A9_Panel_B.pdf", replace

restore
